package com.syqc.utils

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.syqc.comlib.utils.sub
import com.syqc.monitor.R

/**
 * 颜色工具
 */
object ColorUtil {
    private val plateColors = HashMap<String, String>()
    private val plateColorMap = HashMap<String, String>()

    init {
        initColorMap()
        initPlateColorMap()
    }

    /**
     * 初始化车牌颜色
     */
    private fun initPlateColorMap() {
        if (plateColors.isNotEmpty()) return
        plateColors["1"] = "#0523ee"
        plateColors["2"] = "#fbbb07"
        plateColors["3"] = "#060102"
        plateColors["4"] = "#fafbf8"
        plateColors["5"] = "#019166"
        plateColors["9"] = "#b9b9b9"
        plateColors["91"] = "#fbbb07"
        plateColors["92"] = "#019166"
    }

    private fun initColorMap() {
        if (plateColorMap.isNotEmpty()) return
        plateColorMap["1"] = "蓝色"
        plateColorMap["2"] = "黄色"
        plateColorMap["3"] = "黑色"
        plateColorMap["4"] = "白色"
        plateColorMap["5"] = "绿色"
        plateColorMap["6"] = "黄绿(6)"
        plateColorMap["9"] = "其它"
        plateColorMap["10"] = "黄绿色"
        plateColorMap["91"] = "农黄"
        plateColorMap["92"] = "农绿"
        plateColorMap["93"] = "黄绿(93)"
        plateColorMap["94"] = "渐变绿"
        plateColorMap["224"] = "黄绿(224)"
        plateColorMap["225"] = "白绿(225)"
    }

    /**
     * 获取车牌颜色
     */
    fun setPlateColor(key: String): String {
        return plateColors[key] ?: "#0523ee"
    }

    private fun getPlateTextColor(key: String): String {
        return when (key) {
            "", "1", "3" -> "#ffffff"
            else -> "#111111"
        }
    }

    /**
     * 是否车牌颜色之外
     *
     * @param key
     * @return
     */
    private fun isOutColor(key: String): Boolean {
        plateColorMap.keys.forEach {
            if (it == key) return false
        }
        return true
    }


    private fun getColorName(colorName: String): String {
        val kh = colorName.contains("（")
        val name: List<String> = if (kh) {
            colorName.split("（")
        } else {
            colorName.split("(")
        }
        val color = name[0]
        plateColorMap.forEach {
            if (it.value.contains(color)) {
                return it.key
            }
        }
        return ""
    }

    /**
     * 车牌颜色
     *
     * @param textView
     * @param plateColor
     * @param plateColorCode
     */
    fun setPlateColor(
        textArea: TextView,
        textNum: TextView,
        plateColor: String?,
        plateColorCode: String?,
        title: String
    ) {
        var colorCode: String = if (plateColorCode.isNullOrEmpty()) {
            NetDataUtil.getColorByName(plateColor ?: "")
        } else {
            plateColorCode
        }
        if (isOutColor(colorCode)) {
            colorCode = getColorName(plateColor ?: "")
        }
        when (colorCode) {
            "6", "10", "93", "224" -> {
                textArea.setBackgroundResource(R.color.color_e8b)
                textNum.setBackgroundResource(R.color.color_4ec)
            }

            "225" -> {
                textArea.setBackgroundResource(R.color.color_fbf)
                textNum.setBackgroundResource(R.color.color_52f)
            }

            "94" -> {
                textArea.setBackgroundResource(R.drawable.ic_color_plate_94)
                textNum.setBackgroundResource(R.drawable.ic_color_plate_94)
            }

            else -> {
                val colorDrawable = ColorDrawable()
                colorDrawable.color = Color.parseColor(setPlateColor(colorCode ?: ""))
                textArea.background = colorDrawable
                textNum.background = colorDrawable
            }
        }
        if (title.length > 2) {
            textArea.text = title.substring(0, 2)
            textNum.text = sub(title.substring(2), 8)
        } else {
            textArea.text = title
            textNum.text = ""
        }
        textArea.setTextColor(Color.parseColor(getPlateTextColor(colorCode)))
        textNum.setTextColor(Color.parseColor(getPlateTextColor(colorCode)))
    }

    fun lineState(text: TextView, state: String) {
        val toInt = state.toInt()
        val desc: String
        val descColor: Int

        if (toInt == 6 || toInt == 2) {
            desc = text.resources.getString(R.string.desc_list_monitor_arrears)
            descColor = R.color.color_ee9
        } else if (toInt <= 4) {
            desc = text.resources.getString(R.string.desc_list_monitor_offline)
            descColor = R.color.color_999
        } else {
            desc = text.resources.getString(R.string.desc_list_monitor_online)
            descColor = R.color.color_37D
        }
        text.text = " $desc"
        text.setTextColor(ContextCompat.getColor(text.context, descColor))
    }

}